
#include "w25qxx.h"

#ifdef BOARD_USING_SPI_FLASH


#include "zos.h"

zos_uint16_t w25qxx_TYPE=W25Q128;	//默认是W25Q128

//4Kbytes为一个Sector
//16个扇区为1个Block
//W25Q256
//容量为32M字节,共有512个Block,8192个Sector 
													 
//初始化SPI FLASH的IO口
void w25qxx_init(void)
{ 
    zos_uint8_t temp,cmd;
    ZOS_SPI_CONFIG spi_cfg;

	spi_cfg.clk_div = ZOS_SPI_CLK_DIV2;
	spi_cfg.clk_mode = ZOS_SPI_CLK_MODE0;

	zos_spi_init(ZOS_SPI_BUS_0,&spi_cfg);
	w25qxx_TYPE=w25qxx_read_ID();	        //读取FLASH ID.
    if(w25qxx_TYPE==W25Q256)                //SPI FLASH为W25Q256
    {
        temp=w25qxx_read_SR(3);              //读取状态寄存器3，判断地址模式
        if((temp&0X01)==0)			        //如果不是4字节地址模式,则进入4字节地址模式
		{
			cmd=W25X_Enable4ByteAddr;
			zos_spi_cs_enable(ZOS_SPI_BUS_0);
			zos_spi_write(ZOS_SPI_BUS_0,&cmd,1,HAL_MAX_DELAY);//发送进入4字节地址模式指令 
			zos_spi_cs_disable(ZOS_SPI_BUS_0);  
		}
    }
}  

//读取w25qxx的状态寄存器，w25qxx一共有3个状态寄存器
//状态寄存器1：
//BIT7  6   5   4   3   2   1   0
//SPR   RV  TB BP2 BP1 BP0 WEL BUSY
//SPR:默认0,状态寄存器保护位,配合WP使用
//TB,BP2,BP1,BP0:FLASH区域写保护设置
//WEL:写使能锁定
//BUSY:忙标记位(1,忙;0,空闲)
//默认:0x00
//状态寄存器2：
//BIT7  6   5   4   3   2   1   0
//SUS   CMP LB3 LB2 LB1 (R) QE  SRP1
//状态寄存器3：
//BIT7      6    5    4   3   2   1   0
//HOLD/RST  DRV1 DRV0 (R) (R) WPS ADP ADS
//regno:状态寄存器号，范:1~3
//返回值:状态寄存器值
zos_uint8_t w25qxx_read_SR(zos_uint8_t regno)   
{  
	zos_uint8_t cmd[2];
	zos_uint8_t byte=0; 
    switch(regno)
    {
        case 1:
            cmd[0]=W25X_ReadStatusReg1;    //读状态寄存器1指令
            break;
        case 2:
            cmd[0]=W25X_ReadStatusReg2;    //读状态寄存器2指令
            break;
        case 3:
            cmd[0]=W25X_ReadStatusReg3;    //读状态寄存器3指令
            break;
        default:
            cmd[0]=W25X_ReadStatusReg1;    
            break;
    }
	cmd[1]=0xff;
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
	zos_spi_write(ZOS_SPI_BUS_0,&cmd[0],1,HAL_MAX_DELAY);
	zos_spi_read(ZOS_SPI_BUS_0,&cmd[1],1,&byte,1,HAL_MAX_DELAY);
	zos_spi_cs_disable(ZOS_SPI_BUS_0);
	return byte;   
} 
//写w25qxx状态寄存器
void w25qxx_write_SR(zos_uint8_t regno,zos_uint8_t sr)   
{   
    zos_uint8_t cmd[2];
    switch(regno)
    {
        case 1:
            cmd[0]=W25X_WriteStatusReg1;    //写状态寄存器1指令
            break;
        case 2:
            cmd[0]=W25X_WriteStatusReg2;    //写状态寄存器2指令
            break;
        case 3:
            cmd[0]=W25X_WriteStatusReg3;    //写状态寄存器3指令
            break;
        default:
            cmd[0]=W25X_WriteStatusReg1;    
            break;
    }
	cmd[1] = sr;
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
	zos_spi_write(ZOS_SPI_BUS_0,&cmd,2,HAL_MAX_DELAY);
	zos_spi_cs_disable(ZOS_SPI_BUS_0);     
}   
//w25qxx写使能	
//将WEL置位   
void w25qxx_write_enable(void)   
{
	zos_uint8_t cmd=W25X_WriteEnable;
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
	zos_spi_write(ZOS_SPI_BUS_0,&cmd,1,HAL_MAX_DELAY);
	zos_spi_cs_disable(ZOS_SPI_BUS_0);
} 
//w25qxx写禁止	
//将WEL清零  
void w25qxx_write_disable(void)   
{  
	zos_uint8_t cmd=W25X_WriteDisable;
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
	zos_spi_write(ZOS_SPI_BUS_0,&cmd,1,HAL_MAX_DELAY);
	zos_spi_cs_disable(ZOS_SPI_BUS_0);	     	      
} 

//读取芯片ID
//返回值如下:				   
//0XEF13,表示芯片型号为W25Q80  
//0XEF14,表示芯片型号为W25Q16    
//0XEF15,表示芯片型号为W25Q32  
//0XEF16,表示芯片型号为W25Q64 
//0XEF17,表示芯片型号为W25Q128 	  
//0XEF18,表示芯片型号为W25Q256
zos_uint16_t w25qxx_read_ID(void)
{
	zos_uint16_t Temp = 0;	  
	zos_uint8_t cmd[5],read_cmd,byte;
	cmd[0]=0x90;
	cmd[1]=0x00;
	cmd[2]=0x00;
	cmd[3]=0x00;
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
	zos_spi_write(ZOS_SPI_BUS_0,&cmd,4,HAL_MAX_DELAY);	
	read_cmd=0xff;
	zos_spi_read(ZOS_SPI_BUS_0,&read_cmd,1,&byte,1,HAL_MAX_DELAY);
	Temp|=byte<<8;
	zos_spi_read(ZOS_SPI_BUS_0,&read_cmd,1,&byte,1,HAL_MAX_DELAY);
	zos_spi_cs_disable(ZOS_SPI_BUS_0);
	Temp|=byte;
	return Temp;
}   		    
//读取SPI FLASH  
//在指定地址开始读取指定长度的数据
//read_addr:开始读取的地址(24bit)
//read_buf:数据存储区
//read_len:要读取的字节数(最大65535)
void w25qxx_read(zos_uint32_t read_addr,zos_uint8_t* read_buf,zos_uint16_t read_len)
{ 
 	zos_uint16_t i=0;
	zos_uint8_t cmd[5],byte,read_cmd=W25X_ReadData;	
	zos_spi_cs_enable(ZOS_SPI_BUS_0);								    
    zos_spi_write(ZOS_SPI_BUS_0,&read_cmd,1,HAL_MAX_DELAY);	      //发送读取命令  
    if(w25qxx_TYPE==W25Q256)                //如果是W25Q256的话地址为4字节的，要发送最高8位
    {
		cmd[i]=(zos_uint8_t)(read_addr>>24);
        i++;;
    }
	cmd[i]=(zos_uint8_t)(read_addr>>16);
	cmd[i+1]=(zos_uint8_t)(read_addr>>8);
	cmd[i+2]=(zos_uint8_t)(read_addr);

    zos_spi_write(ZOS_SPI_BUS_0,&cmd,i+3,HAL_MAX_DELAY); 
	cmd[4]=0xFF;
    for(i=0;i<read_len;i++)
	{ 
		zos_spi_read(ZOS_SPI_BUS_0,&cmd[4],1,&read_buf[i],1,HAL_MAX_DELAY);//循环读数 
    }
	zos_spi_cs_disable(ZOS_SPI_BUS_0);			    	      
}  
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//write_addr:开始写入的地址(24bit)
//write_buf:数据存储区
//write_len:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!	 
void w25qxx_write_page(zos_uint32_t write_addr,zos_uint8_t* write_buf,zos_uint16_t write_len)
{
 	zos_uint16_t i=0;
	zos_uint8_t cmd[5],byte,read_cmd=W25X_PageProgram; 	
    w25qxx_write_enable();                  //SET WEL  
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
    zos_spi_write(ZOS_SPI_BUS_0,&read_cmd,1,HAL_MAX_DELAY);   //发送写页命令   
    if(w25qxx_TYPE==W25Q256)                //如果是W25Q256的话地址为4字节的，要发送最高8位
    {
		cmd[i]=(zos_uint8_t)((write_addr)>>24);
		i++;
    }
	cmd[i]=(zos_uint8_t)(write_addr>>16);
	cmd[i+1]=(zos_uint8_t)(write_addr>>8);
	cmd[i+2]=(zos_uint8_t)(write_addr);

	zos_spi_write(ZOS_SPI_BUS_0,&cmd,i+3,HAL_MAX_DELAY); 
	for(i=0;i<write_len;i++)
	{
		zos_spi_write(ZOS_SPI_BUS_0,&write_buf[i],1,HAL_MAX_DELAY); 
	}
	
	zos_spi_cs_disable(ZOS_SPI_BUS_0);
	w25qxx_wait_busy();					   //等待写入结束
} 
//无检验写SPI FLASH 
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能 
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//write_addr:开始写入的地址(24bit)
//write_buf:数据存储区
//write_len:要写入的字节数(最大65535)
//CHECK OK
void w25qxx_write_no_check(zos_uint32_t write_addr,zos_uint8_t* write_buf,zos_uint16_t write_len)   
{ 			 		 
	zos_uint16_t pageremain;   
	pageremain=256-write_addr%256; //单页剩余的字节数		 	    
	if(write_len<=pageremain)pageremain=write_len;//不大于256个字节
	while(1)
	{	   
		w25qxx_write_page(write_addr,write_buf,pageremain);
		if(write_len==pageremain)break;//写入结束了
	 	else //write_len>pageremain
		{
			write_buf+=pageremain;
			write_addr+=pageremain;	

			write_len-=pageremain;			  //减去已经写入了的字节数
			if(write_len>256)pageremain=256; //一次可以写入256个字节
			else pageremain=write_len; 	  //不够256个字节了
		}
	};	    
} 
//写SPI FLASH  
//在指定地址开始写入指定长度的数据
//该函数带擦除操作!
//write_addr:开始写入的地址(24bit)
//write_buf:数据存储区					
//write_len:要写入的字节数(最大65535)   
zos_uint8_t w25qxx_BUFFER[4096];		 
void w25qxx_write(zos_uint32_t write_addr,zos_uint8_t* write_buf,zos_uint16_t write_len)   
{ 
	zos_uint32_t secpos;
	zos_uint16_t secoff;
	zos_uint16_t secremain;	   
 	zos_uint16_t i;    
	zos_uint8_t * w25qxx_BUF;	  
   	w25qxx_BUF=w25qxx_BUFFER;	     
 	secpos=write_addr/4096;//扇区地址  
	secoff=write_addr%4096;//在扇区内的偏移
	secremain=4096-secoff;//扇区剩余空间大小   
 	//printf("ad:%X,nb:%X\r\n",write_addr,write_len);//测试用
 	if(write_len<=secremain)secremain=write_len;//不大于4096个字节
	while(1) 
	{	
		w25qxx_read(secpos*4096,w25qxx_BUF,4096);//读出整个扇区的内容
		for(i=0;i<secremain;i++)//校验数据
		{
			if(w25qxx_BUF[secoff+i]!=0XFF)break;//需要擦除  	  
		}
		if(i<secremain)//需要擦除
		{
			w25qxx_erase_sector(secpos);//擦除这个扇区
			for(i=0;i<secremain;i++)	   //复制
			{
				w25qxx_BUF[i+secoff]=write_buf[i];	  
			}
			w25qxx_write_no_check(secpos*4096,w25qxx_BUF,4096);//写入整个扇区  

		}else w25qxx_write_no_check(write_addr,write_buf,secremain);//写已经擦除了的,直接写入扇区剩余区间. 				   
		if(write_len==secremain)break;//写入结束了
		else//写入未结束
		{
			secpos++;//扇区地址增1
			secoff=0;//偏移位置为0 	 

		   	write_buf+=secremain;  //指针偏移
			write_addr+=secremain;//写地址偏移	   
		   	write_len-=secremain;				//字节数递减
			if(write_len>4096)secremain=4096;	//下一个扇区还是写不完
			else secremain=write_len;			//下一个扇区可以写完了
		}	 
	};	 
}
//擦除整个芯片		  
//等待时间超长...
void w25qxx_erase_chip(void)   
{
	zos_uint8_t cmd;                  
    w25qxx_write_enable();                  //SET WEL 
    w25qxx_wait_busy();
	cmd=W25X_ChipErase;  
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
    zos_spi_write(ZOS_SPI_BUS_0,&cmd,1,HAL_MAX_DELAY);        //发送片擦除命令 
	zos_spi_cs_disable(ZOS_SPI_BUS_0);   	      
	w25qxx_wait_busy();   				   //等待芯片擦除结束
}   
//擦除一个扇区
//dst_Addr:扇区地址 根据实际容量设置
//擦除一个扇区的最少时间:150ms
void w25qxx_erase_sector(zos_uint32_t dst_Addr)   
{  
	//监视falsh擦除情况,测试用   
 	//printf("fe:%x\r\n",dst_Addr);	
	zos_uint8_t cmd[5],i=0,read_cmd=W25X_SectorErase;  
 	dst_Addr*=4096;
    w25qxx_write_enable();                  //SET WEL 	 
    w25qxx_wait_busy();
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
    zos_spi_write(ZOS_SPI_BUS_0,&read_cmd,1,HAL_MAX_DELAY);   //发送扇区擦除指令 

	if(w25qxx_TYPE==W25Q256)                //如果是W25Q256的话地址为4字节的，要发送最高8位
    {
		cmd[i]=(zos_uint8_t)((dst_Addr)>>24);
		i++;
    }
	cmd[i]=(zos_uint8_t)(dst_Addr>>16);
	cmd[i+1]=(zos_uint8_t)(dst_Addr>>8);
	cmd[i+2]=(zos_uint8_t)(dst_Addr);

	zos_spi_write(ZOS_SPI_BUS_0,&cmd,i+3,HAL_MAX_DELAY); 
	zos_spi_cs_disable(ZOS_SPI_BUS_0); 	      
    w25qxx_wait_busy();   				    //等待擦除完成
}  
//等待空闲
void w25qxx_wait_busy(void)   
{   
	while((w25qxx_read_SR(1)&0x01)==0x01);   // 等待BUSY位清空
}  
//进入掉电模式
void w25qxx_power_down(void)   
{ 
	zos_uint8_t cmd=W25X_PowerDown;
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
	zos_spi_write(ZOS_SPI_BUS_0,&cmd,1,HAL_MAX_DELAY);    
	zos_spi_cs_disable(ZOS_SPI_BUS_0);	      
    zos_task_delay_us(3);                            //等待TPD  
}   
//唤醒
void w25qxx_wakeup(void)   
{  
	zos_uint8_t cmd=W25X_ReleasePowerDown;
	zos_spi_cs_enable(ZOS_SPI_BUS_0);
	zos_spi_write(ZOS_SPI_BUS_0,&cmd,1,HAL_MAX_DELAY);  
	zos_spi_cs_disable(ZOS_SPI_BUS_0);  	      
    zos_task_delay_us(3);                            //等待TPD  
}   


#endif


